//
//  VisitorView.m
//  我的微博
//
//  Created by tom-1 on 15/12/6.
//  Copyright © 2015年 tom-1. All rights reserved.
//

#import "VisitorView.h"
#import "UIButton+Category.h"
#import "UILabel+Category.h"

@implementation VisitorView
// 代理方法
- (void)clickRegister {
    [self.delegate visitorViewDidRegister];
}
- (void)clickLogin {
    [self.delegate visitorVIewDidLogin];
}

#pragma mark - 设置视图信息
- (void)setupInfo:(NSString *)imageName title:(NSString *)title {
    self.messageLabel.text = title;
    // 如果图片名称为空 说明是首页直接返回
    if (imageName == nil) {
        // 播放动画
        [self startAnim];
        return;
    }
    self.iconView.image = [UIImage imageNamed:imageName];
    // 隐藏小房子
    self.homeIconView.hidden = YES;
    // 将遮罩视图移动到底部
    [self sendSubviewToBack:self.maskIconView];
    

    
}
// 开启首页转轮动画
- (void)startAnim {
    // 核心动画
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim.toValue = [NSNumber numberWithDouble:M_PI * 2];
    anim.repeatCount = MAXFLOAT;
    anim.duration = 20;
    // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
    anim.removedOnCompletion = NO;
    // 添加到图层
    [self.iconView.layer addAnimation:anim forKey:nil];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}



#pragma mark - 懒加载控件
// 图标
- (UIImageView *)iconView {
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    }
    return _iconView;
}
// 遮罩图像
- (UIImageView *)maskIconView {
    if (_maskIconView == nil) {
        _maskIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    return _maskIconView;
}
// 小房子
- (UIImageView *)homeIconView {
    if (_homeIconView == nil) {
        _homeIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    }
    return _homeIconView;
}
// 消息文字
- (UILabel *)messageLabel {
    if (_messageLabel == nil) {
        _messageLabel = [UILabel labelWithFontColorTitle:@"关注一些人,回这里看看有什么惊喜" font:14 color:[UIColor darkGrayColor]];

    }
    return _messageLabel;
}
// 注册按钮
- (UIButton *)registerButton {
    if (_registerButton == nil) {
        _registerButton = [UIButton buttonWithColorImageTitle:@"注册" color:[UIColor orangeColor] image:@"common_button_white_disable"];
        
    }
    return _registerButton;
}
// 登录按钮
- (UIButton *)loginButton {
    if (_loginButton == nil) {
        _loginButton = [UIButton buttonWithColorImageTitle:@"登录" color:[UIColor orangeColor] image:@"common_button_white_disable"];

    }
    return _loginButton;
}
#pragma mark - 设置界面
- (void)setupUI {
    [self addSubview:self.iconView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.homeIconView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    // NO 表示控件支持自动布局来完成
    for (UIView *v in self.subviews) {
        v.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    // 图标
//    self.iconView.frame = CGRectMake(100, 100, 100, 100);
    NSLayoutConstraint *leftCons = [NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    [self addConstraint:leftCons];
    NSLayoutConstraint *bottomCons = [NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-60];
    [self addConstraint:bottomCons];
    // 小房子
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeIconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeIconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    // 消息文字
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:224]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:36]];
    // 注册按钮
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:36]];
    // 登录按钮
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:36]];
    
    
    // 遮罩视图
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[mask]-0-|" options:0 metrics:nil views:@{@"mask": self.maskIconView}]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[mask]-(btnHeight)-[regButton]" options:0 metrics:@{@"btnHeight": @(-36)} views:@{@"mask": self.maskIconView, @"regButton": self.registerButton}]];
    
     // 背景颜色
    
    self.backgroundColor = [UIColor colorWithWhite:237.0 / 255.0 alpha:1.0];
    
    // 注册监听方法
    [self.registerButton addTarget:self action:@selector(clickRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.loginButton addTarget:self action:@selector(clickLogin) forControlEvents:UIControlEventTouchUpInside];
}
@end
